import type { APIResourceItem } from '$lib/api/types';
import { writable } from 'svelte/store';
import { simpleRandomId } from '$lib/utils/textKit.svelte'

// 处理左侧选择的接口api
export const selectedAPIItem = writable<APIResourceItem | null>(null);


// 处理用户输入的headers、params、body等请求数据
const requestPayload = {
    headers: {},
    params: {},
    data: {},
    body: '' // 可以是字符串（如 JSON）或 FormData 对象
};

// 创建可写 Store
export const requestStore = writable(requestPayload);

// 定义操作函数
export const updateHeaders = (newHeaders: { [key: string]: any }) => {
    requestStore.update(store => ({
        ...store,
        headers: { ...newHeaders }
    }));
};

export const updateParams = (newParams: { [key: string]: any }) => {
    requestStore.update(store => ({
        ...store,
        params: { ...newParams }
    }));
};

export const updateData = (newData: { [key: string]: any }) => {
    requestStore.update(store => ({
        ...store,
        data: { ...newData }
    }));
};

export const updateBody = (newBody: any) => {
    requestStore.update(store => ({
        ...store,
        body: newBody
    }));
};

// 处理实时的域名地址信息
export const FullURL = writable('')

// 接收API接口响应信息
export const APIResponse = writable({})



export let BaseUrls = $state([
    { id: simpleRandomId(), label: 'DEV', url: "http://localhost:996", placeholder: '开发环境基础URL' },
    { id: simpleRandomId(), label: 'STG', url: "", placeholder: '测试环境基础URL' },
    { id: simpleRandomId(), label: 'PRD', url: "", placeholder: '生产环境基础URL' },
  ])
  
export const ENV_STORAGE_KEY = 'paramkit_envs'


